package com.backend.appointmanage.service.impl;

import java.util.Date;
import java.util.List;

import com.backend.mbuser.domain.MobileUser;
import com.backend.room.domain.Room;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.backend.appointmanage.mapper.ViewingAppointmentManageViewMapper;
import com.backend.appointmanage.domain.ViewingAppointmentManageView;
import com.backend.appointmanage.service.IViewingAppointmentManageViewService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 看房预约管理Service业务层处理
 * 
 * @author fusi
 * @date 2024-10-12
 */
@Service
public class ViewingAppointmentManageViewServiceImpl implements IViewingAppointmentManageViewService 
{
    @Autowired
    private ViewingAppointmentManageViewMapper viewingAppointmentManageViewMapper;

    @Scheduled(fixedRate = 10000)   //10000：10s一次
    public void checkAppointmentStatusTask(){
        Date now = new Date();
        List<ViewingAppointmentManageView> list = viewingAppointmentManageViewMapper.selectViewingAppointmentManageViewStatusList();
        for (ViewingAppointmentManageView viewingAppointmentManageView : list) {
            if(viewingAppointmentManageView.getAppointmentTime().before(now) && !"2".equals(viewingAppointmentManageView.getStatus())){
                viewingAppointmentManageView.setStatus("2");
                viewingAppointmentManageViewMapper.updateViewingAppointment(viewingAppointmentManageView);
            }else if(viewingAppointmentManageView.getAppointmentTime().after(now) && "2".equals(viewingAppointmentManageView.getStatus())){
                viewingAppointmentManageView.setStatus("1");
                viewingAppointmentManageViewMapper.updateViewingAppointment(viewingAppointmentManageView);
            }
        }
    }

    /**
     * 查询看房预约管理
     *
     *
     * @return 看房预约管理
     */
    @Override
    public ViewingAppointmentManageView selectViewingAppointmentManageViewByViewingAppointmentId(Long viewingAppointmentId)
    {
        return viewingAppointmentManageViewMapper.selectViewingAppointmentManageViewByViewingAppointmentId(viewingAppointmentId);
    }
    @Override
    public MobileUser selectMobileUserByMobileUserId(Long mobileUserId) {
        return viewingAppointmentManageViewMapper.selectMobileUserByMobileUserId(mobileUserId);
    }

    @Override
    public Room selectRoomByRoomId(Long roomId) {
        return viewingAppointmentManageViewMapper.selectRoomByRoomId(roomId);
    }

    /**
     * 查询看房预约管理列表
     *
     * @param viewingAppointmentManageView 看房预约管理
     * @return 看房预约管理
     */
    @Override
    public List<ViewingAppointmentManageView> selectViewingAppointmentManageViewList(ViewingAppointmentManageView viewingAppointmentManageView)
    {
        return viewingAppointmentManageViewMapper.selectViewingAppointmentManageViewList(viewingAppointmentManageView);
    }

    /**
     * 新增看房预约管理
     * 
     * @param viewingAppointmentManageView 看房预约管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertViewingAppointmentManageView(ViewingAppointmentManageView viewingAppointmentManageView)
    {
        viewingAppointmentManageView.setViewingAppointmentId(viewingAppointmentManageViewMapper.getMaxViewingAppointmentId() + 1);
        int a = viewingAppointmentManageViewMapper.insertViewingAppointment(viewingAppointmentManageView);
        int b = viewingAppointmentManageViewMapper.insertMbuserApplyAppointment(viewingAppointmentManageView);
        int c = viewingAppointmentManageViewMapper.insertAppointmentCorrespondRoom(viewingAppointmentManageView);
        return a>0 && b>0 && c>0?a+b+c:0;
    }

    /**
     * 修改看房预约管理
     * 
     * @param viewingAppointmentManageView 看房预约管理
     * 在前端移动端用户id和房间id是必填项
     * @return 结果
     */
    @Override
    @Transactional
    public int updateViewingAppointmentManageView(ViewingAppointmentManageView viewingAppointmentManageView) {
        int b = 0,c = 0;
        int a = viewingAppointmentManageViewMapper.updateViewingAppointment(viewingAppointmentManageView);
        if(viewingAppointmentManageView.getMobileUserId() != null)
            b = viewingAppointmentManageViewMapper.updateMbuserApplyAppointment(viewingAppointmentManageView);
        if(viewingAppointmentManageView.getRoomId() != null)
            c = viewingAppointmentManageViewMapper.updateAppointmentCorrespondRoom(viewingAppointmentManageView);
        return a>0?a+b+c:0;
    }
    /**
     * 批量删除看房预约管理
     *
     * @return 结果
     */
    @Override
    public int deleteViewingAppointmentByViewingAppointmentIds(Long[] viewingAppointmentIds)
    {
        return viewingAppointmentManageViewMapper.deleteViewingAppointmentByViewingAppointmentIds(viewingAppointmentIds);
    }

    /**
     * 删除看房预约管理信息
     * 
     * @param applyId 看房预约管理主键
     * @return 结果
     */
    @Override
    public int deleteViewingAppointmentManageViewByApplyId(Long applyId)
    {
        return viewingAppointmentManageViewMapper.deleteViewingAppointmentManageViewByApplyId(applyId);
    }
}
